#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
运行Panel拼接脚本的简化版本
"""
import os
import sys
import subprocess

def main():
    """运行panel拼接脚本"""
    print("🚀 Panel拼接脚本启动器")
    print("=" * 60)
    
    # 设置路径
    script_dir = os.path.dirname(os.path.abspath(__file__))
    panel_splicing_script = os.path.join(script_dir, "panel_splicing.py")
    
    print(f"📁 脚本目录: {script_dir}")
    print(f"🐍 目标脚本: {panel_splicing_script}")
    
    # 检查脚本是否存在
    if not os.path.exists(panel_splicing_script):
        print(f"❌ 错误: 找不到脚本文件 {panel_splicing_script}")
        return
    print("✅ 脚本文件检查通过")
    
    # 设置默认参数
    panel_dir = "/Users/pppsy/code/python/ForensicsGenome/code/splicing/2010072670_FG_004/panel/2010072670_FG_004_5_1"
    output_dir = "/Users/pppsy/code/python/ForensicsGenome/code/splicing/2010072670_FG_004/synthetic_panels"
    mask_output_dir = "/Users/pppsy/code/python/ForensicsGenome/code/splicing/2010072670_FG_004/masks"
    forgery_json = "/Users/pppsy/code/python/ForensicsGenome/code/splicing/forgery.json"
    
    print("📋 运行参数:")
    print(f"   📁 Panel目录: {panel_dir}")
    print(f"   📁 输出目录: {output_dir}")
    print(f"   📁 Mask目录: {mask_output_dir}")
    print(f"   📄 Forgery JSON: {forgery_json}")
    
    # 检查GPU可用性
    print("🔍 检查GPU可用性...")
    gpu_available = os.system("nvidia-smi > /dev/null 2>&1") == 0
    device = "cuda" if gpu_available else "cpu"
    print(f"   🖥️  设备: {device} ({'GPU可用' if gpu_available else '仅CPU'})")
    
    # 构建命令
    cmd = [
        sys.executable, panel_splicing_script,
        "--panel_dir", panel_dir,
        "--output_dir", output_dir,
        "--mask_output_dir", mask_output_dir,
        "--forgery_json", forgery_json,
        "--method", "similarity",
        "--device", device
    ]
    
    print("=" * 60)
    print("🚀 正在启动panel拼接脚本...")
    print(f"💻 执行命令: {' '.join(cmd)}")
    print("=" * 60)
    
    try:
        # 运行脚本
        result = subprocess.run(cmd, check=True, capture_output=True, text=True)
        print("=" * 60)
        print("✅ 脚本执行成功!")
        print("=" * 60)
        print("📄 脚本输出:")
        print(result.stdout)
        
    except subprocess.CalledProcessError as e:
        print("=" * 60)
        print(f"❌ 脚本执行失败: {e}")
        print("=" * 60)
        print("📄 错误输出:")
        print(e.stderr)
    except Exception as e:
        print("=" * 60)
        print(f"❌ 运行过程中出错: {e}")
        print("=" * 60)

if __name__ == "__main__":
    main()
